const app = getApp()
const util = require("../../utils/utils.js")

// pages/secondhand-cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedIndex: [],
    checked: [],
    userNicknames: [],
    items: [
      //   {
      //   itemid: 'itemid',
      //   imgUrl: "/assets/product_img/pro1.jpg",
      //   title: 'title',
      //   price: '200.00',
      //   userinfo: {
      //     username: 'username',
      //     avatarUrl: "/assets/avatars/avatar1.jpg" 
      //   }
      // },{
      //   itemid: 'itemid',
      //   imgUrl: "/assets/product_img/pro1.jpg",
      //   title: 'title',
      //   price: '200.00',
      //   userinfo: {
      //     username: 'username',
      //     avatarUrl: "/assets/avatars/avatar1.jpg" 
      //   }
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "购物车"
    });
    var that = this
    wx.request({
      url: 'http://localhost:8888/cart/getCartByUser',
      method: 'GET',
      data: {
        user: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          items: res.data.data,
        })
        for (var i = 0; i < that.data.items.length; i++) {
          var iPhoto = "items[" + i + "].photo"
          that.setData({
            [iPhoto]: util.strToArr(that.data.items[i].photo)[0],
          })
        }
        for (var i = 0; i < that.data.items.length; i++) {
          that.getNicknames(that.data.items[i].username, i)
        }
      }
    })
  },

  getNicknames(id, i) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var iNickname = "userNicknames[" + i + "]"
        that.setData({
          [iNickname]: res.data.data.username
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onCheckChange(e) {
    //console.log(e.detail.value);
    this.setData({
      selectedIndex: e.detail.value
    })
    //console.log(this.data.selectedIndex);
  },
  outCart() {
    var indexArray = this.data.selectedIndex
    var itemArray = this.data.items
    var nickArray = this.data.userNicknames
    for (var i = 0; i < indexArray.length; i++) {
      var index = indexArray[i]
      this.removeCart(this.data.items[index].id)
      itemArray[index] = null
      nickArray[index] = null
    }
    itemArray = itemArray.filter(n => n)
    nickArray = nickArray.filter(n => n)
    this.setData({
      items: itemArray,
      userNicknames: nickArray,
    })
    var checked = this.data.checked
    checked.length = 0
    that.setData({
      checked: checked
    })
    for(var i=0;i<this.data.items.length;i++) {
      var iChecked = "checked["+i+"]"
      that.setData({
        [iChecked]: false
      })
    }
  },
  checkCart() {
    var indexArray = this.data.selectedIndex
    var itemArray = this.data.items
    var nickArray = this.data.userNicknames
    for (var i = 0; i < indexArray.length; i++) {
      var index = indexArray[i]
      this.removeCart(this.data.items[index].id)
      this.insertBuying(this.data.items[index].id, index)
      itemArray[index] = null
      nickArray[index] = null
    }
    itemArray = itemArray.filter(n => n)
    nickArray = nickArray.filter(n => n)
    this.setData({
      items: itemArray,
      userNicknames: nickArray,
    })
    var checked = this.data.checked
    checked.length = 0
    that.setData({
      checked: checked
    })
    for(var i=0;i<this.data.items.length;i++) {
      var iChecked = "checked["+i+"]"
      that.setData({
        [iChecked]: false
      })
    }
  },
  removeCart(id) {
    wx.request({
      url: 'http://localhost:8888/cart/deleteCart',
      method: 'POST',
      data: {
        user: app.globalData.loginAccount,
        dealing: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.showToast({
          title: '移出购物车成功',
          icon: "success"
        })
      }
    })
  },
  insertBuying(id, index) {
    var seller = this.data.items[index].username
    var itemname = this.data.items[index].itemname
    wx.request({
      url: 'http://localhost:8888/buying/insertBuying',
      method: 'POST',
      data: {
        username: app.globalData.loginAccount,
        dealing: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.request({
          url: 'http://localhost:8888/chatting/sendMsg',
          method: 'POST',
          data: {
            user1: "sys",
            user2: seller,
            content: "您的商品（"+itemname+"）被购买，请及时查看"
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
          }
        })
        wx.showToast({
          title: '提交请求成功',
          icon: "success"
        })
      }
    })
  }
})